from imagetools import ImageTools

import Image

class ScaledCopies(ImageTools):
    
    def __init__(self,photo,scales=['',400,100]):
        """
            @param photo:    the original to be scaled and coppied out
            @param scales:    a list of the sizes to save the image as - the first element should be ''
                              to preserve the original image 
        """
        #@todo: change the behavor from always assuming the coordinate provided is vertical
        # to taking in a pair of values
        super(ScaledCopies,self).__init__(photo)
        self.scales=scales        
        self.names=[]
        self.urls=[]
        self.paths=[]
    def saveCopies(self,photo=None,scales=None):
        """
            If the first element of the scales array is not 'orig' then 
            the first size is taken and overrites the original uploaded file
            so the 'orig' parameter ensures that the original image is preserved.
        """
        if photo!=None:
            self.image=photo
        if scales!=None:
            self.scales=scales
        self.getconfig(image=self.image)
        
        original = Image.open(self.path)
        original = original.convert("RGB") 
        i=0
        for scale in self.scales:
            h1 = scale
            if h1 != '':
                w,h = original.size
                w1=w*h1/h
                resized = original.resize((w1,h1),Image.ANTIALIAS)
                if i!=0:
                    resized_path = self.path+str(scale)+'.jpeg'
                    self.names.append(self.name+str(scale)+'.jpeg')
                    self.paths.append(resized_path)
                    self.urls.append(self.url+str(scale)+'.jpeg')
                else: #if its the first member of the scale list, and we didn't get skipped over, overwrite the orig
                    resized_path=self.path
                
                resized.save(resized_path,'JPEG')
            i=i+1
        return (self.names,self.urls)
            
